<?php

/**
 * Authentication
 *
 * @category   app_C
 * @package    app_C
 * @author     
 * @copyright  Copyright (c)
 * @license
 */
class AuthController extends Zend_Controller_Action
{
    /**
     * Initialize object.
     *
     * @return void
     */
	function init()
    {
        $this->db = Zend_Registry::get('db');
        $this->smarty = Zend_Registry::get('smarty');
        Zend_Loader::loadClass('Album');
    }
    
    /**
     * we just redirect back to the default if someone does navigate
     * to auth/index.
     *
     * @return void
     */
    function indexAction()
    {
        $this->_redirect('/');
    }
    
    /**
     * Logging In.
     *
     * @return void
     */
    function loginAction()
    {
    	$this->smarty->assign('message','');
        $this->smarty->assign('title','Log in');
         
        if ($this->_request->isPost()) {
            // collect the data from the user
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $f = new Zend_Filter_StripTags();
            $username = $f->filter($this->_request->getPost('username'));
            $password = $f->filter($this->_request->getPost('password'));
            
            if (empty($username)) {
            	$this->smarty->assign('message','Please provide a username.');
            } else {
                // setup Zend_Auth adapter for a database table
                Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
                $db = Zend_Registry::get('db');
                $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                
                $authAdapter->setTableName('users');
                $authAdapter->setIdentityColumn('username');
                $authAdapter->setCredentialColumn('password');
                
                // Set the input credential values to authenticate against
                $authAdapter->setIdentity($username);
                $authAdapter->setCredential($password);
                
                // do the authentication
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                if ($result->isValid()) {
                    // success: store database row to auth's storage
                    // system. (Not the password though!)
                    $data = $authAdapter->getResultRowObject(null, 'password');
                    $auth->getStorage()->write($data);
                    $this->_redirect('/');
                } else {
                    // failure: clear database row from session
                    $this->smarty->assign('message','Login failed.');
                }
            }
        }
        $this->smarty->display('login.tpl');
    }
    
    /**
     * Logging out.
     *
     * @return void
     */
    function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/');
    }
}